<template>
  <div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import * as echarts from 'echarts'
import mapJson from '@/assets/MapData/china.json'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

console.log(props.data)
const target = ref(null)
let myChart = null
const render = () => {
  const options = {
    timeline: {
      data: props.data.voltageLevel,
      axisType: 'category',
      autoPlay: true,
      playInterval: 3000,
      left: '10%',
      right: '10%',
      bottom: '0%',
      width: '80%',
      label: {
        normal: {
          textStyle: {
            color: '#ddd'
          }
        },
        emphasis: {
          textStyle: {
            color: '#fff'
          }
        }
      },
      symbolSize: 10,
      lineStyle: { color: '#555' }
    },
    baseOption: {
      grid: {
        right: '2%',
        top: '15%',
        bottom: '10%',
        width: '20%'
      },
      geo: {
        show: true,
        map: 'china',
        roam: true,
        zoom: 0.8,
        center: [113.83531246, 34.0267395887],
        itemStyle: {
          normal: {
            borderColor: 'rgba(147, 235, 248, 1)',
            areaColor: {
              type: 'radial',
              x: 0.5,
              y: 0.5,
              r: 0.5,
              colorStops: [
                // 0% 处的颜色
                { offset: 0, color: 'rgba(147, 235, 248, 0)' },
                // 100% 处的颜色
                { offset: 1, color: 'rgba(147, 235, 248, .2)' }
              ],
              globalCoord: false
            }
          },
          emphasis: { areaColor: '#389BB7', borderWidth: 0 }
        }
      }
    },
    options: []
  }
  props.data.voltageLevel.forEach((item, index) => {
    options.options.push({
      backgroundColor: '#142037',
      title: [
        // 主标题，对应地图
        {
          text: '2019-2023 年度数据统计',
          left: '0%',
          top: '0',
          textStyle: {
            color: '#ccc',
            fontSize: 30
          }
        },
        // 副标题，对应柱形图
        {
          id: 'statistic',
          text: item + '年数据统计情况',
          right: '0%',
          top: '4%',
          textStyle: {
            color: '#ccc',
            fontSize: 20
          }
        }
      ],
      xAxis: {
        position: 'top',
        type: 'value',
        axisTick: { show: false },
        axisLine: { show: false },
        splitLine: {
          show: false
        },
        axisLabel: {
          margin: 0,
          interval: 0,
          textStyle: {
            color: '#aaa'
          }
        }
      },
      yAxis: {
        axisTick: { show: false },
        axisLabel: {
          interval: 0,
          textStyle: {
            color: '#ddd'
          }
        },
        data: props.data.categoryData[item].map((item) => item.name)
      },
      series: [
        {
          type: 'bar',
          data: props.data.categoryData[item].map((item) => item.value),
          itemStyle: {
            color: props.data.colors[index]
          }
        },
        {
          type: 'effectScatter',
          coordinateSystem: 'geo',
          data: props.data.topData[item],
          symbolSize: function (val) {
            return val[2] / 4
          },
          rippleEffect: {
            brushType: 'stroke'
          },
          label: {
            normal: {
              formatter: '{b}',
              position: 'right',
              show: true
            }
          },
          itemStyle: {
            color: props.data.colors[index],
            // 阴影配置
            shadowBlur: 5,
            shadowColor: props.data.colors[index]
          }
        }
      ]
    })
  })
  myChart.setOption(options)
}
onMounted(() => {
  echarts.registerMap('china', mapJson)
  myChart = echarts.init(target.value)
  render()
})
</script>

<style lang="scss" scoped></style>
